package com.yuezhi.consumer.service;

import com.yuezhi.consumer.constant.QueueConstant;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class RPCService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RabbitListener(queues = QueueConstant.RPC_QUEUE_ONE)
    public void process(Message message) {
        byte[] body = message.getBody();
        Message build = MessageBuilder.withBody(("我是服务端，收到了客户端发来的消息：" + new String(body)).getBytes()).build();
        CorrelationData correlationData = new CorrelationData(message.getMessageProperties().getCorrelationId());
        Message receive = rabbitTemplate.sendAndReceive(QueueConstant.RPC_EXCHANGE, QueueConstant.RPC_QUEUE_TWO, build, correlationData);
        System.out.println(receive);
    }
}
